<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { ElMessage } from "element-plus"
import { formatDateTime } from "@/utils"

// 接口
import { getMsg, backMsg } from "@/api/advice"
import { usePagination } from "@/hooks/usePagination"
import { Search, Refresh } from "@element-plus/icons-vue"
import { useUserStore } from "@/store/modules/user"
import { sizeDate, Y, M, D, shortcuts } from "@/utils/dateSet"
const loading = ref(true)
const userStore = useUserStore()
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
defineOptions({
  name: "adviceList"
})
const defaultTime1: [Date, Date] = [new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)]
// #region 搜索内容
const searchFormRef = ref<any>(null)
const searchData = reactive<any>({
  massage: "",
  searchDate: [
    new Date(Y.value, M.value, D.value, 0, 0, 0).getTime() - 3600 * 1000 * 24 * 7,
    new Date(Y.value, M.value, D.value, 23, 59, 59)
  ],
  types: ""
})
const columns = ref<any>([
  { text: "求助", value: "求助" },
  { text: "咨询", value: "咨询" },
  { text: "投诉", value: "投诉" },
  { text: "建议", value: "建议" },
  { text: "其它", value: "其它" }
])
// #region 查
const tableTotal = ref<number>()
const tableData = ref<any>([])
const getList = () => {
  getMsg({
    page: paginationData.currentPage,
    size: paginationData.pageSize,
    massage: searchData.massage,
    types: searchData.types,
    start: searchData.searchDate && searchData.searchDate.length ? new Date(searchData?.searchDate[0]).getTime() : "",
    end: searchData.searchDate && searchData.searchDate.length ? new Date(searchData?.searchDate[1]).getTime() : ""
  })
    .then((res: any) => {
      tableData.value = res.data.list
      tableTotal.value = res.data.total
      loading.value = false
    })
    .catch(() => {
      tableData.value = []
      loading.value = false
    })
}
// #endregion

const resetSearch = () => {
  searchFormRef.value?.resetFields()
  if (paginationData.currentPage === 1) {
    getList()
    console.log("重置")
  }
  paginationData.currentPage = 1
}
const handleSearch = () => {
  if (paginationData.currentPage === 1) {
    getList()
    console.log("查询")
  }
  paginationData.currentPage = 1
}
//清除查询日期
const clearDate = (val: any) => {
  // console.log(val)
  if (!val) {
    searchData.searchDate = []
  }
}
/**回复信件 */
const formRef = ref<any>(null)
const formData = reactive<any>({
  contents: "",
  backContents: ""
})
const rules = reactive<any>({
  backContents: [{ required: true, trigger: "blur", message: "回复内容不能为空" }]
})
const dialogVisible = ref<boolean>(false)
const currentId = ref<any>(undefined)
const clearValidate = () => {
  formRef.value?.clearValidate()
}
const restForm = () => {
  clearValidate()
  formRef.value?.resetFields()
}
const handleBack = (row: any) => {
  dialogVisible.value = true
  currentId.value = row.id
  formData.contents = row.contents
}
const handleCreate = () => {
  formRef.value?.validate((valid: boolean) => {
    if (valid) {
      backMsg({
        id: currentId.value,
        backContents: formData.backContents,
        backIdent: userStore.username
      }).then((res: any) => {
        if (res.code == 200) {
          dialogVisible.value = false
          ElMessage.success(res.data)
          getList()
        }
      })
    }
  })
}
// #endregion
/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getList, { immediate: true })
</script>

<template>
  <div class="app-container">
    <el-card shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="types" label="类型">
          <el-select v-model="searchData.types" clearable filterable placeholder="请选择类型" style="width: 240px">
            <el-option v-for="item in columns" :key="item.value" :label="item.value" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item prop="massage" label="反馈内容">
          <el-input v-model.trim="searchData.massage" clearable placeholder="请输入" style="width: 240px" />
        </el-form-item>
        <el-form-item prop="searchDate" label="时间">
          <el-date-picker
            v-model="searchData.searchDate"
            type="datetimerange"
            unlink-panels
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            :shortcuts="shortcuts"
            :size="sizeDate"
            :default-time="defaultTime1"
            @change="clearDate"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 中间部分 -->
    <el-card v-loading="loading" shadow="never">
      <!-- 中间渲染内容 -->
      <div class="table-center">
        <el-table :data="tableData">
          <el-table-column prop="id" label="ID" width="120" align="center" />
          <el-table-column prop="createTime" label="创建时间" align="center">
            <template #default="scope">
              {{ formatDateTime(scope.row.createTime) }}
            </template>
          </el-table-column>
          <el-table-column prop="types" label="类型" align="center" />
          <el-table-column prop="title" label="标题" align="center" />
          <el-table-column prop="contents" label="反馈内容" align="center" />
          <el-table-column prop="backCreateTime" label="回复时间" align="center">
            <template #default="scope">
              <span v-if="scope.row.backCreateTime"> {{ formatDateTime(scope.row.backCreateTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="backContents" label="回复内容" align="center" />
          <!-- <el-table-column prop="ident" label="标识" align="center" /> -->
          <el-table-column fixed="right" label="操作" width="150" align="left">
            <template #default="scope">
              <p>
                <el-button type="primary" text bg size="small" @click="handleBack(scope.row)">回复</el-button>
              </p>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页器 -->
      <div class="pagination">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :page-size="paginationData.pageSize"
          :total="tableTotal"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <el-dialog v-model="dialogVisible" @close="restForm">
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px" label-position="left">
        <el-form-item prop="contents" label="反馈内容">
          <el-input type="textarea" :rows="5" v-model="formData.contents" disabled />
        </el-form-item>
        <el-form-item prop="backContents" label="回复内容">
          <el-input type="textarea" :rows="5" v-model="formData.backContents" placeholder="请回复" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreate">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
// .box-card {
//   height: 70px;
//   margin-bottom: 20px;
// }
.search-wrapper {
  margin-bottom: 20px;
}
.middle-top {
  margin-bottom: 20px;
}
.table-center {
  margin-bottom: 20px;
}
.pagination {
  display: flex;
  justify-content: flex-end;
}
</style>
